package com.winphone.uavc.controller;

import com.winphone.uavc.entity.SysUser;
import com.winphone.uavc.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author: zhou
 * @Description:
 * @Date:Create in 2017/11/10
 * @Modified By:
 */
@Controller
public class HomeController {

    @Autowired
    ISysUserService sysUserService;

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = {"/index","/"})
    public String index(HttpServletResponse response){
      /* Session session =  SecurityUtils.getSubject().getSession();
       SysUser sysUser = SessionUtil.getCurrentUser();
       Long sessionId = SessionCacheUtil.getLoginUserId();*/
        return "index";
    }

    @RequestMapping("/loginUser")
    public String loginUser(String username,String password,HttpSession session,HttpServletRequest request) {
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//        token.setRememberMe(true);
        Subject subject = SecurityUtils.getSubject();
        try {
            if (!subject.isAuthenticated()){
                subject.login(token);           //完成登录
            }
            SysUser user = (SysUser) subject.getPrincipal();
            System.out.println("session"+session.getId());
            session.setAttribute("user", user);
            return "redirect:index";
        }catch (LockedAccountException lae) {
            token.clear();
            request.setAttribute("msg", "用户已经被锁定不能登录，请与管理员联系！");
            return "login";
        } catch (AuthenticationException e) {
            token.clear();
            request.setAttribute("msg", "用户或密码不正确！");
            return "login";
        }

    }

    @RequestMapping("/logOut")
    public String logOut(HttpSession session) {
        session.removeAttribute("user");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    /**
     * 用户管理
     */
    @RequestMapping(value = "sysUser/page",method = RequestMethod.GET)
    public String sysUser() {
        return "admin/user/list";
    }

    /**
     * 组织结构
     */
    @RequestMapping(value = "organization/page",method = RequestMethod.GET)
    public String organization() {
        return "admin/organization/list";
    }

    /**
     * 账户信息
     */
    @RequestMapping(value = "account/page",method = RequestMethod.GET)
    public String userInfo() {
        return "admin/account/info";
    }

}
